/***************************************************************************
*  Mainwidget.h
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifndef MainWidget_H
#define MainWidget_H
#include <QDialog>
#include <QWidget>
#include <QList>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include "Configurebase.h"
#include "ConfigGlobal.h"
#include "Editproxyservers.h"
#include "Clientview.h"
#include "Abstractclientelement.h"
#include "Edithistory.h"
#include "ui_Mainwidget.h"
#define LINKTARGETIMAGE ":/icons/linktargetimage.png"
#define EXECUTIVETARGETIMAGE ":/icons/excutivetargetimage.png"
#define UNKOWNTARGETIMAGE ":/icons/unkowntargetimage.png"
#define LIBPATH "LD_PRELOAD=/usr/lib/libflealib.so.0.9.1 "
typedef QList<AbstractClientElement *> AbstractClient;
class QMouseEvent;
class QDragEnterEvent;
class QDropEvent;
class QSize;
class QPoint;
class QPixmap;
class QBitmap;
class QPalette;
class QTimerEvent;

class MainWidget:public QDialog, public Ui::FormMain
{
        Q_OBJECT
public:

       MainWidget(QWidget *parent = 0,Qt::WindowFlags f = 0);
       ~MainWidget();
       void handleSubProcessExit(pid_t pid);
       static void waitForSubProcessExit(MainWidget *main);
       void showError(QString error);

protected:

       void mouseMoveEvent(QMouseEvent *e);
       void mousePressEvent(QMouseEvent *e);
       void dragEnterEvent(QDragEnterEvent *event);
       void dropEvent(QDropEvent *event);

signals:
		void subProcessExit(pid_t pid);
       void errorHappenedInSubThread(QString error);
		void setCheckedStatus();
		void analyseThisTarget(QString path);
		void startClientProcess(QString exec,QString icon);
		void readyToAddNewClient(QString clientname,QString image,pid_t pid);
		void addHistory(QString path);
		
		void doThisPreparation(int code);// this signal calls programe to execute
		//  some interactive configurations what it needs to run properly.
		// 0 -  configure the proxy server list
		// 1 -  configure the global settings
		// 
public slots:

       bool loadProxyServer();
       bool loadConfig();
       void exitNow();
protected slots:
		void  exeThisPreparation(int code);
	    void setMenuShown(bool shown );
       void  manageClient(bool shown);
       void  manageSettings(bool shown);
       void  manageProxyServers(bool shown);
       void  manageHistory(bool shown);
       void  changeCheckedStatus();
       void  showAboutMe();
       void analyseTarget(QString path);
       bool processHandler(QString exec,QString icon);
       void addClientToList(QString clientname,QString image,pid_t pid);
       void addToHistory(QString path);
       //////////////
      
       ////////////////
       void subButtonClicked();
private:
	    static void exeThread(MainWidget *main);
private:  
       QPoint last,pos0;
       int origwidth;
       int origHeight;
       int compleWidth;
       int menuHeight;
       int compleHeight;
       
       pthread_mutex_t mut;
       QString startprocesserror;
       QString proxylist;
       QWidget *curWidget;
       char **argtag;
       int enabledToWork;// used to tag weather netflea is ready to work
       ///// different value stands for differe meaning:
       // 0 - ready to work
        //  1 - global settings not configured,not ready to work
        //   2 -  no proxy servers provided,not ready to work
          //   3  - both "global settings not configured" and "no proxy servers provided"
       ConfigureBase *config;
       AbstractClient clientlist;
       int menuCheckedIndex;// the value defined as follows:
      // -1 :no menu button was checked
       // 0 :the button "toolButtonEditSettings" has been checked;
       // 1: the button "toolButtonProxyServers" has been checked;
       // 2: the button "toolButtonProxyClients" has been checked;

};

#endif
